﻿package  {
	import com.kerry.character.Role;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.net.URLRequest;
	import flash.ui.Keyboard;
	
	/**
	 * com.kerry.character.Role 类的演示文件，人物行走
	 * @author PhoenixKerry（http://blog.sina.com.cn/yyy98）
	 */
	public class RoleDemo extends Sprite {
		private var dirX:int = 0;
		private var dirY:int = 0;
		private var role:Role;
		
		public function RoleDemo() {
			var loader:Loader = new Loader();
			addChild(loader);
			//loader.load(new URLRequest("../assets/role-4.png"));
			loader.load(new URLRequest("../assets/role-8.png"));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
		}
		
		private function onImageLoaded(e:Event):void {
			var loaderInfo:LoaderInfo = e.target as LoaderInfo;
			var bitmap:Bitmap = loaderInfo.content as Bitmap;
			var bitmapData:BitmapData = bitmap.bitmapData;
			//role = new Role(bitmapData, 4, 4, 5, false, 3, 0, 1);
			role = new Role(bitmapData, 5, 9, 5);
			addChild(role);
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler);
			addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
		}
		
		private function onEnterFrameHandler(e:Event):void {
			role.go(dirX, dirY);
		}
		
		private function onKeyUpHandler(e:KeyboardEvent):void {
			switch (e.keyCode) {
				case Keyboard.UP:
				case Keyboard.DOWN: dirY = 0;
				break;
				case Keyboard.LEFT:
				case Keyboard.RIGHT: dirX = 0;
			}
		}
		
		private function onKeyDownHandler(e:KeyboardEvent):void {
			switch (e.keyCode) {
				case Keyboard.UP: dirY = -1;
				break;
				case Keyboard.DOWN: dirY = 1;
				break;
				case Keyboard.LEFT: dirX = -1;
				break;
				case Keyboard.RIGHT: dirX = 1;
			}
		}
		
	}

}